package com.muyu.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;

import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.domain.DanmoMarketOrder;
import com.muyu.system.domain.DanmoMarketProducts;
import com.muyu.system.dto.DanmoMarketProductsDTO;
import com.muyu.system.dto.OrderStatusCountDTO;
import com.muyu.system.dto.UpdatePriceDTO;
import com.muyu.system.req.DanmoMarketProductsReq;
import com.muyu.system.req.DanmoProvincesCities;
import com.muyu.system.service.IDanmoMarketProductsService;

import com.muyu.system.vo.OrderStatusCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 书画市场商品信息Controller
 *
 * @author yangle
 * @date 2024-09-07
 */
@RestController
@RequestMapping("/markProducts")
public class DanmoMarketProductsController extends BaseController {

    @Autowired
    private IDanmoMarketProductsService danmoMarketProductsService;

    /**
     * 查询书画市场商品信息列表
     */
//    @RequiresPermissions("system:products:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoMarketProductsDTO danmoMarketProducts) {
        PageHelper.startPage(danmoMarketProducts.getPageNum(), danmoMarketProducts.getPageSize());
        if (danmoMarketProducts.getMarketType() == null || danmoMarketProducts.getMarketType() !=6){
            List<DanmoMarketProducts> list = danmoMarketProductsService.selectDanmoMarketProductsList(danmoMarketProducts);
            PageInfo<DanmoMarketProducts> danmoMarketProductsPageInfo = new PageInfo<>(list);
            return Result.success(danmoMarketProductsPageInfo);
       }else {
            List<DanmoMarketProducts> list = danmoMarketProductsService.selectDanmoMarketProductsList(danmoMarketProducts);
            PageInfo<DanmoMarketProducts> danmoMarketProductsPageInfo = new PageInfo<>(list);
            return Result.success(danmoMarketProductsPageInfo);
       }
    }

    /**
     * 导出书画市场商品信息列表
     */
//    @RequiresPermissions("system:products:export")
    @Log(title = "书画市场商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoMarketProductsDTO danmoMarketProducts) {
        List<DanmoMarketProducts> list = danmoMarketProductsService.selectDanmoMarketProductsList(danmoMarketProducts);
        ExcelUtil<DanmoMarketProducts> util = new ExcelUtil<DanmoMarketProducts>(DanmoMarketProducts.class);
        util.exportExcel(response, list, "书画市场商品信息数据");
    }

    /**
     * 获取书画市场商品信息详细信息
     */
//    @RequiresPermissions("system:products:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return success(danmoMarketProductsService.selectDanmoMarketProductsById(id));
    }

    /**
     * 新增书画市场商品信息
     */
//    @RequiresPermissions("system:products:add")
    @Log(title = "书画市场商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoMarketProducts danmoMarketProducts) {

        return danmoMarketProductsService.insertDanmoMarketProducts(danmoMarketProducts);
    }

    /**
     * 修改书画市场商品信息
     */
//    @RequiresPermissions("system:products:edit")
    @Log(title = "书画市场商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody DanmoMarketProducts danmoMarketProducts) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoMarketProducts.setUpdateBy(String.valueOf(loginUser.getUserid()));
        return danmoMarketProductsService.updateDanmoMarketProducts(danmoMarketProducts);
    }


    /**
     * 修改价格
     * @param updatePriceDTO
     * @return
     */
    @PostMapping("/update/price")
    public Result<String> updatePrice(@RequestBody @Valid UpdatePriceDTO updatePriceDTO){
        danmoMarketProductsService.updatePrice(updatePriceDTO);
        return Result.success("修改成功！");
    }





    /**
     * 删除书画市场商品信息
     */
//    @RequiresPermissions("system:products:remove")
    @Log(title = "书画市场商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return danmoMarketProductsService.deleteDanmoMarketProductsByIds(ids);
    }

    /**
     * 下架书画市场商品信息
     */
    @PostMapping("removed/{id}")
    public Result removed(@PathVariable Long id) {
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }

        // 获取商品信息
        DanmoMarketProducts danmoMarketProducts = danmoMarketProductsService.selectDanmoMarketProductsById(id);
        if (danmoMarketProducts == null) {
            return Result.error("商品不存在");
        }

        // 检查商品当前状态是否已下架
        if (danmoMarketProducts.getMarketState() != null && danmoMarketProducts.getMarketState() == 2L) {
            return Result.error("商品已经是下架状态");
        }

        // 设置下架状态
        danmoMarketProducts.setMarketState(2L);
        danmoMarketProducts.setCreateBy(String.valueOf(loginUser.getUserid()));

        return danmoMarketProductsService.updateDanmoMarketProducts(danmoMarketProducts);
    }

    /**
     * 重新上架书画市场商品信息
     */
    @PostMapping("listed/{id}")
    public Result listed(@PathVariable Long id) {
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }

        // 获取商品信息
        DanmoMarketProducts danmoMarketProducts = danmoMarketProductsService.selectDanmoMarketProductsById(id);
        if (danmoMarketProducts == null) {
            return Result.error("商品不存在");
        }

        // 检查商品当前状态是否已上架
        if (danmoMarketProducts.getMarketState() != null && danmoMarketProducts.getMarketState() == 1L) {
            return Result.error("商品已经是上架状态");
        }

        // 设置上架状态
        danmoMarketProducts.setMarketState(1L);
        danmoMarketProducts.setCreateBy(String.valueOf(loginUser.getUserid()));

        // 执行更新
        return danmoMarketProductsService.updateDanmoMarketProducts(danmoMarketProducts);
    }

    /**
     * 价格排序
     */
    @PostMapping("/priceSort")
    public Result priceSort(@RequestBody DanmoMarketProducts danmoMarketProducts) {
        List<DanmoMarketProducts> danmoMarketProductsList = danmoMarketProductsService.priceSort(danmoMarketProducts);
        return success(danmoMarketProductsList);
    }

    /**
     * 搜索省市
     */
    @PostMapping("/selectDanmoProvincesCities")
    public Result selectDanmoProvincesCities(@RequestBody DanmoProvincesCities danmoProvincesCities) {
        List<DanmoMarketProducts> danmoMarketProductsList = danmoMarketProductsService.selectDanmoProvincesCities(danmoProvincesCities);
        return success(danmoMarketProductsList);
    }

    /**
     * 按照时间排序
     */
    @PostMapping("/selectDanmoLatestTime")
    public Result selectDanmoLatestTime(@RequestBody DanmoMarketProducts danmoMarketProducts) {
        List<DanmoMarketProducts> danmoMarketProductsList = danmoMarketProductsService.selectDanmoLatestTime(danmoMarketProducts);
        return success(danmoMarketProductsList);
    }

    /**
     * 查询单个商品
     */
    @PostMapping("selectDanmoMarketProducts")
    public Result selectDanmoMarketProducts(@RequestBody DanmoMarketProductsReq danmoMarketProductsReq) {
        DanmoMarketProducts danmoMarketProductsd = danmoMarketProductsService.selectDanmoMarketProducts(danmoMarketProductsReq);
        return success(danmoMarketProductsd);
    }


    /**
     * 购买商品
     */
    @PostMapping("/purchaseGoods")
    public Result addMarket(@RequestBody DanmoMarketProducts danmoMarketProducts) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoMarketProductsService.addMarket(danmoMarketProducts);
    }


    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody DanmoMarketProducts danmoMarketProducts) {
        return danmoMarketProductsService.addOrder(danmoMarketProducts);
    }

    /**
     * 查询单个订单
     */
    @PostMapping("/selectSingleOrder")
    public Result selectSingleOrder(@RequestBody DanmoMarketProductsReq danmoMarketProductsReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoMarketProductsReq.setId(Long.valueOf(danmoMarketProductsReq.getProductId()));
        DanmoMarketOrder danmoMarketOrder = danmoMarketProductsService.selectSingleOrder(danmoMarketProductsReq);
        return Result.success(danmoMarketOrder);
    }

    /**
     * 我的商品
     */
    @PostMapping("/myOrder")
    public Result myOrder(@RequestBody DanmoMarketProductsReq danmoMarketProductsReq) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoMarketProductsReq.setUserId(loginUser.getUserid());
        return danmoMarketProductsService.selectMyOrder(danmoMarketProductsReq);
    }

    /**
     * 他人商品
     */
    @PostMapping("/otherOrder")
    public Result otherOrder(@RequestBody DanmoMarketProductsReq danmoMarketProductsReq) {
        return danmoMarketProductsService.selectOtherOrder(danmoMarketProductsReq);
    }

    /**
     * 他人
     */
    @PostMapping("/other")
    public Result other(@RequestBody DanmoMarketProductsReq danmoMarketProductsReq) {
        Long id = danmoMarketProductsReq.getInformationId();
        return danmoMarketProductsService.selectOther(id);
    }


    /**
     * 订单状态统计
     * @param orderStatusCountDTO
     * @return
     */
    @PostMapping("/order/status/count")
    public Result<OrderStatusCountVO> orderStatusCount(@RequestBody @Valid OrderStatusCountDTO orderStatusCountDTO){
        return Result.success(danmoMarketProductsService.orderStatusCount(orderStatusCountDTO));
    }







}